package com.infore.airsupervise.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.github.gcacace.signaturepad.views.SignaturePad;
import com.infore.airsupervise.R;
import com.infore.airsupervise.other.listener.SignatrueBiz;
import com.infore.airsupervise.other.listener.SignatrueBizImpl;
import com.infore.airsupervise.other.listener.SignatrueViewListener;

/**
 * 签名版 的 布局视图
 * Created by xiaotao on 2017/5/17.
 */

public class SignatrueLayout implements View.OnClickListener ,SignaturePad.OnSignedListener{
    private SignaturePad mSignaturePad;
    private Button clearBtn, saveBtn;

    private Context context;
    private View contentView;
    private SignatrueBiz signatrueBiz; //视图操作处理业务
    private SignatrueViewListener listener;

    public SignatrueLayout(Context context) {
        this.context = context;
        contentView = LayoutInflater.from(context).inflate(R.layout.layout_signatrue,null);
         mSignaturePad = (SignaturePad) contentView.findViewById(R.id.signature_pad);//签名板
        clearBtn = (Button) contentView.findViewById(R.id.clear_button); //清清除
        saveBtn = (Button) contentView.findViewById(R.id.save_button); //保存
        clearBtn.setOnClickListener(this);
        saveBtn.setOnClickListener(this);
        mSignaturePad.setOnSignedListener(this);
        signatrueBiz = new SignatrueBizImpl();
    }

    public void setSignatrueViewListener(SignatrueViewListener listener) {
        this.listener = listener;
    }

    /**
     * 获取布局
     * @return
     */
    public  View getContentView(){
        return contentView;
    }

    /**
     * 点击事件监听
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.clear_button:
                mSignaturePad.clear();
                if(null != listener){
                    signatrueBiz.ClearSignatrue(listener);
                }

                break;

            case R.id.save_button:
                Bitmap signatureBitmap = mSignaturePad.getSignatureBitmap();
                if(null != listener){
                    signatrueBiz.SaveSignatrue(signatureBitmap,listener);
                }
                break;
        }
    }

    /**
     * 签名版 监听
     */
    @Override
    public void onStartSigning() {

    }

    @Override
    public void onSigned() {
        clearBtn.setEnabled(true);
        saveBtn.setEnabled(true);
    }

    @Override
    public void onClear() {
        clearBtn.setEnabled(false);
        saveBtn.setEnabled(false);
    }

}
